package com.woniuxy.dudugua.controller;

import com.woniuxy.dudugua.entity.User;
import com.woniuxy.dudugua.service.UserCouponsService;
import com.woniuxy.dudugua.util.Result;
import com.woniuxy.dudugua.util.view.CouponsStoreView;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("coupon")
public class CouponsController {
    @Autowired
    private UserCouponsService userCouponsService;
    @RequestMapping("getusercoupons")
    public Result selectAllCouponsByUserId(){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        List<CouponsStoreView> list = userCouponsService.selectAllCoupons(user.getId());
        return new Result("200","success",null,list);
    }
    @RequestMapping("getallcoupons")
    public Result selectAllCouponsView(){
        List<CouponsStoreView> list = userCouponsService.selectAllCoupons(0);
        return new Result("200","success",null,list);
    }
    @RequestMapping("usergetcoupons")
    public Result userGetCoupons(Integer couponid){
        User user = (User)SecurityUtils.getSubject().getPrincipal();
        System.out.println(couponid);
        System.out.println(user.getId());
        int i = userCouponsService.userGetCoupons(user.getId(), couponid);
        String message = "";
        if(i == 1){
            message = "领取成功";
        }
        if(i == 2){
            message = "已达领取上限";
        }
        if(i == 3){
            message = "优惠券已领取完";
        }
        return new Result("200",message,null,null);
    }
}
